<?php
require_once("classes/Pageley/Util/System.php");

class Pageley_Test_Runner {
	public function runMain() {
        echo "------------------------------------------------------------------------\n";
        echo " T E S T S\n";
        echo "------------------------------------------------------------------------\n";
	    $allSuites = new Pageley_Util_ArrayMap();
		$system = Pageley_Util_System::getInstance();
		$testSuites = $system->getProperty("testSuites");
		if($testSuites === null) {
			$testSuites = ".";
		}
		$fileDelimiter = Pageley_Util_System::getInstance()->getProperty("file.delimiter");
		$suites = preg_split("/" . $fileDelimiter . "/", $testSuites);
		foreach($suites as $suite) {
			$suite = Pageley_Util_String::replace($suite, "\\\\", "/");
			$allSuites->add($suite);
		}
		$results = $this->run($allSuites);
		if ($results->isEmpty()) {
			echo "No tests to run";
		}
		echo "------------------------------------------------------------------------\n";
		echo " T E S T   R E S U L T S\n";
        echo "------------------------------------------------------------------------\n";
        $failures = false;
		foreach($results as $result) {
			echo $result . "\n";
			if($result->getSuccessCount() < $result->getTestCount()) {
				$failures = true;
			}
		}
        echo "------------------------------------------------------------------------\n";
		if($failures) {
	        echo " T E S T   F A I L U R E\n";
	        exit(1);
		}
	}
	
	public function run($suites) {
		$suiteResults = new Pageley_Util_ArrayMap();
		foreach($suites as $suite) {
			$testSuite = new Pageley_Test_Suite($suite);
			$suiteResults->add($testSuite->run());
		}
		return $suiteResults;
	}
}

$system = Pageley_Util_System::getInstance();
$system->runMain(new Pageley_Test_Runner());